var layer;
var $;
var element;
var form;
var laydate;
var table;
layui.use(['element','layer','laydate','form','table'], function(){
    layer = layui.layer;
    $ = layui.jquery;
    element = layui.element;
    form = layui.form;
    laydate = layui.laydate;
    table = layui.table;
    //执行一个laydate实例
    laydate.render({
        elem: '#createTime', //指定元素
        type: 'date',
        trigger: 'click' //时间时间为点击事件
    });
    load();
    selIndexUserTab();
})
/**
 *  点击事件  刷新  返回
 *  高级检索
 * */
function load(){
    $(".tit_btn").bind('click',function(){
        location.reload();
    })
    $(".tit_btn2").bind('click',function(){
        window.history.go(-1);  //返回上一页
    })
    $("#index_btn1").bind('click',function(){
        layer.open({
            type:2,//弹出层的类型
            title:"高级检索",
            area:['460px','360px'],//弹出层大小
            shade:0,//设置遮罩
            offset:'auto',//设置遮罩的位置
            maxmin:true,
            //弹出层内容
            content :"/personnel/layer/PerUserList_layer_index.html",
            btn:['确定','取消'],
            yes:function(index,layero){
                var body = layero.find('iframe').contents();
                var form = $(body).find('form').serialize();
                var userId = body.find("input[name=userId]").val();
                var consumption = body.find("input[name=consumption]").val();
                var studyDate = body.find("input[name=studyDate]").val();
                if(!userId){
                    form+="&userId="+0
                }else{
                    form+="&userId="+userId
                }
                if(!consumption){
                    form+="&consumption="+0
                }else{
                    form+="&consumption="+consumption
                }
                if(!studyDate){
                    form+="&studyDate="+0
                }else{
                    form+="&studyDate="+studyDate
                }
                selIndexUserTab(form);
                layer.close(layer.index);
            },btn2:function(index,layero){

            }
        })
    })
    //点击搜索
    $("#indexBtn").bind('click',function(){
        selIndexUserTab();
        layer.msg("搜索成功",{icon: 1});
    })
    //点击赠送优惠券
    $("#index_btn2").bind('click',function(){
        giveAwayCoupons();
    })
    //点击添加进思用户人员
    $("#index_btn3").bind('click',function(){
        addThinkUser();
    })
}
/**
 *  赠送优惠券
 */
function giveAwayCoupons(){
    var checkStatus = table.checkStatus('userListTab');
    var ids = [];
    $(checkStatus.data).each(function (i, o) {//o即为表格中一行的数据
        ids.push(o.userId);
    });
    ids = ids.join(",");
    if (ids.length < 1) {
        layer.msg('请选择所要赠送的学生', {icon: 7});
        return false;
    }else{
        layer.open({
            type:2,//弹出层的类型
            title:"赠送优惠券",
            area:['980px','540px'],//弹出层大小
            shade:0,//设置遮罩
            offset:'auto',//设置遮罩的位置
            maxmin:true,
            //弹出层内容
            content :"/personnel/layer/PerUserList_Think_layer_GiveAwayCoupons.html",
            success:function(layero,index){
                var body = layero.find('iframe').contents();
                body.find("input[name=userId]").val(ids);
            }
        })
    }
}
/**
 *  点击添加为进思用户
 * */
function addThinkUser(){
    var checkStatus = table.checkStatus('userListTab');
    var uids = [];
    $(checkStatus.data).each(function (i, o) {//o即为表格中一行的数据
        uids.push(o.userId);
    });
    if (uids.length < 1) {
        layer.msg('请选择所要添加的用户', {icon: 7});
        return false;
    }else if(uids.length>1){
        layer.msg("只能选取一个用户",{icon:7});
    }else{
        var userId = uids[0];
        var form = "&userId="+0;
        $.post("/personnel/selThinkUserTable", form, function (res) {
            var data = res.obj;
            var obj = [];
            for(var a=0;a<data.length;a++){
                obj.push(data[a].userId1);
            }
            var obj2 = JSON.stringify(obj);
            //includes判断数组中是否有该元素  true  false
            if(obj2.includes(userId)){
                layer.msg("该用户已是进思学员，请重新选择",{icon:7});
            }else{
                addThinkUserInfo(userId);
            }
        });

    }
}
function addThinkUserInfo(userId){
    localStorage.setItem("userIDs",userId);
    var layer1 = layer.open({
        type:2,//弹出层的类型
        title:"添加进思用户信息",
        area:['800px','550px'],//弹出层大小
        shade:0,//设置遮罩
        offset:'auto',//设置遮罩的位置
        maxmin:true,
        //弹出层内容
        content :"/personnel/layer/PerUserList_layer_addThinkInfo.html",
        btn:['添加','取消'],
        yes:function(index,layero){
            var body = layero.find('iframe').contents();
            var studentNumber = body.find("input[name=studentNumber]").val();
            var profession = body.find("input[name=profession]").val();
            var integration = body.find("input[name=integration]").val();
            var type = body.find("select[name=type]").val();
            var entranceDate = body.find("input[name=entranceDate]").val();
            var graduationDate = body.find("input[name=graduationDate]").val();
            var studyContent = body.find("textarea[name=studyContent]").val();
            var create=localStorage.getItem("user");
            create=JSON.parse(create);//将获取json字符串转json对象
            var createId = create.userId;
            if(!createId){
                form+="&createId="+0
            }else{
                form+="&createId="+createId
            }
            if(!userId){
                form+="&userId="+0
            }else{
                form+="&userId="+userId
            }
            if(!studentNumber){
                layer.msg("学生学号不能为空",{icon:5});
            }else{
                form+="&studentNumber="+studentNumber
                if(!profession){
                    layer.msg("所属专业不能为空",{icon:5});
                }else{
                    form+="&profession="+profession
                    if(!integration){
                        form+="&integration="+0
                        layer.msg("量化分不能为空",{icon:5});
                    }else{
                        form+="&integration="+integration
                        if(!type){
                            form+="&type="+0
                            layer.msg("请选择是否毕业",{icon:5});
                        }else{
                            form+="&type="+type
                            if(!entranceDate){
                                layer.msg("请选择入学日期",{icon:5});
                            }else{
                                form+="&entranceDate="+entranceDate
                                if(!graduationDate){

                                }else{
                                    form+="&graduationDate="+graduationDate
                                }
                                if(!studyContent){
                                    layer.msg("学习内容不能为空",{icon:5});
                                }else{
                                    form+="&studyContent="+studyContent
                                    layer.confirm('您确定要添加此用户吗?', function (rel) {
                                        $.post("/personnel/addThinkUserInfo", form, function (res) {
                                            if(res){
                                                layer.msg("添加成功",{icon:1});
                                                var userID = localStorage.getItem("userID");
                                                var tablename = "用户数据列表";
                                                var type2=2;
                                                var remark = userID+"";
                                                addOperLog(tablename,type2,remark);
                                                layer.closeAll('iframe');
                                            }else{
                                                layer.msg("添加失败",{icon:2});
                                            }
                                        });
                                    });
                                }
                            }
                        }
                    }
                }
            }
        },btn2:function(){

        },success:function(layero,index){
            var obj = layero.find('iframe').contents();
            var form = "&userId="+userId;
            $.post("/personnel/selUserTable", form, function (res) {
                obj.find("input[name=account]").val(res[0].account);
                obj.find("input[name=nickname]").val(res[0].nickname);
                obj.find("input[name=realName]").val(res[0].realName);
                obj.find("input[name=avatarUrl]").val(res[0].avatarUrl);
                obj.find("input[name=photoImg]").val(res[0].photoImg);
                obj.find("input[name=userId]").val(res[0].userId);
                obj.find(".nickname").html(res[0].nickname);
            })
        }
    })
}
/**
 *  检索用户列表
 */
function selIndexUserTab(vals){
    var account = $("input[name=account]").val();
    if(!account){
        vals+="&account="+''
    }else{
        vals+="&account="+account
    }
    var nickname = $("input[name=nickname]").val();
    if(!nickname){
        vals+="&nickname="+''
    }else{
        vals+="&nickname="+nickname
    }
    var createTime = $("input[name=createTime]").val();
    if(!createTime){
        vals+="&createTime="+''
    }else{
        vals+="&createTime="+createTime
    }
    $.ajax({
        url:"/personnel/selNotThinkUser",
        type:"post",
        dataType:"json",
        data:vals,
        success:function(item) {
            userTab(item.obj);
        }
    })
}
function userTab(item){
    table.render({
        elem:"#userListTab",//容器
        title:"非进思用户数据列表",
        limit:10,//分页显示条数
        width:1225,
        data:item,
        toolbar:true,
        page:true,
        cols:[[//定义列
            {type:'checkbox',align:'center',width:80},//复选框靠左
            {title:'序号',align:'center',templet:function(data){
                    return data.LAY_INDEX;
                }},
            {field:"userId",title:"用户编号",align:'center',width:120},
            {field:"account",title:"用户账号",align:'center',width:140},
            {field:"nickname",title:"用户昵称",align:'center',width:110},
            {field:"studyDate",title:"学习时长",align:'center',width:120,templet:function(data){
                    if(data.studyDate){
                        return data.studyDate+" 分钟";
                    }else{
                        return '0 分钟';
                    }
                }},
            {field:"consumption",title:"用户消费总金额",align:'center',width:120,templet:function(data){
                    if(data.consumption){
                        return data.consumption+" 元";
                    }else{
                        return '0 元';
                    }
                }},
            {field:"createTime",title:"账户注册时间",align:'center',width:165},
            {field:"status",title:"状态",align:'center',width:100,templet:function(data){
                    if(data.status==2){
                        return"<a style='display:inline-block;'><input type=\"checkbox\" disabled  checked lay-skin=\"switch\"  lay-filter='filter' lay-text='正常|停用'>"
                    }else if(data.status==3){
                        return"<a style='display:inline-block;'><input type=\"checkbox\" disabled  lay-skin=\"switch\" lay-filter='filter' lay-text='正常|停用'>"
                    }
                }},
            {field:"",title:"操作",align:'center',width:200,templet:function(data){
                    return "<a style='color:#009688;cursor:pointer;' onclick='selUser(\""+data.userId+"\");'>查看</a>   " +
                        "<a style='color:#009688;cursor:pointer;' onclick='updUser(\""+data.userId+"\");'>编译</a>   " +
                        "<a style='color:#009688;cursor:pointer;' onclick='checkswitchoff(\""+data.userId+"\",\""+data.account+"\",\""+data.status+"\")'>封停</a>  ";
                }}
        ]]
    });
}
/**
 * 查看用户详情信息
 */
function selUser(userId){
    $.ajax({
        url:"/personnel/selUserInfo",
        type:"post",
        dataType:"json",
        data:{"userId":userId},
        success:function(item) {
            showSelUserTab(item);
        }
    })
}
function showSelUserTab(item){
    var tablename = "非进思用户数据列表";
    var type = 5;
    var remark = item.userId+"";
    addOperLog(tablename,type,remark);
    var layer = layui.layer;
    layer.open({
        type:2,//弹出层的类型
        title:"用户信息",
        area:['1100px','650px'],//弹出层大小
        shade:0,//设置遮罩
        offset:'auto',//设置遮罩的位置
        maxmin:true,
        //弹出层内容
        content :"/personnel/layer/PerUserList_layer_showSelUserTab.html",
        btn:['关闭','取消'],
        yes:function(index){
            layer.close(layer.index);
        },btn2:function(index){
            layer.close(layer.index);
        },success:function(layero,index){
            var obj = layero.find('iframe').contents();
            obj.find("input[name=account]").val(item.account);
            obj.find("input[name=nickname]").val(item.nickname);
            obj.find("input[name=userCode]").val(item.userCode);
            obj.find("input[name=studyDate]").val(item.studyDate+"分钟");
            obj.find("input[name=freestyle]").val(item.freestyle);
            obj.find("input[name=idCard]").val(item.idCard);
            obj.find("input[name=phone]").val(item.phone);
            if(item.sex==2){
                obj.find("input[name=sex]").val("男");
            }else if(item.sex==3){
                obj.find("input[name=sex]").val("女");
            }
            obj.find("input[name=email]").val(item.email);
            obj.find("input[name=consumption]").val(item.consumption+"元");
            obj.find("input[name=createTime]").val(item.createTime);
            obj.find("input[name=userId]").val(item.userId);
            obj.find("input[name=avatarUrl]").val(item.avatarUrl);
            obj.find("input[name=photoImg]").val(item.photoImg);
        }
    })
}
/**
 *  编译用户信息 ( 不能编译用户的学习时长  和 交易金额 以及 账号创建时间)
 */
function updUser(userId){
    $.ajax({
        url:"/personnel/selUserInfo",
        type:"post",
        dataType:"json",
        data:{"userId":userId},
        success:function(item) {
            updSelUserTab(item);
        }
    })
}
function updSelUserTab(item){
    var layer = layui.layer;
    layer.open({
        type:2,//弹出层的类型
        title:"用户信息",
        area:['1100px','650px'],//弹出层大小
        shade:0,//设置遮罩
        offset:'auto',//设置遮罩的位置
        maxmin:true,
        //弹出层内容
        content :"/personnel/layer/PerUserList_layer_updSelUserTab.html",
        btn:['更改','取消'],
        yes:function(index,layero){
            var body = layero.find('iframe').contents();
            var form = $(body).find('form').serialize();
            var sex = body.find("input[name=sex]").val();
            var userId = item.userId;
            if(sex=="男"){
                form+="&sex"+2;
                updSelUser(form,userId);
            }else if(sex=="女"){
                form+="&sex"+3;
                updSelUser(form,userId);
            }else{
                form+="&sex"+0;
                updSelUser(form,userId);
            }
            layer.close(layer.index);
        },btn2:function(){

        },success:function(layero,index){
            var obj = layero.find('iframe').contents();
            obj.find("input[name=account]").val(item.account);
            obj.find("input[name=nickname]").val(item.nickname);
            obj.find("input[name=userCode]").val(item.userCode);
            obj.find("input[name=studyDate]").val(item.studyDate+"分钟     （不可修改）");
            obj.find("input[name=freestyle]").val(item.freestyle);
            obj.find("input[name=idCard]").val(item.idCard);
            obj.find("input[name=phone]").val(item.phone);
            if(item.sex==2){
                obj.find("input[name=sex]").val("男");
            }else if(item.sex==3){
                obj.find("input[name=sex]").val("女");
            }
            obj.find("input[name=email]").val(item.email);
            obj.find("input[name=consumption]").val(item.consumption+"元     （不可修改）");
            obj.find("input[name=createTime]").val(item.createTime+"     （不可修改）");
            obj.find("input[name=userId]").val(item.userId);
            obj.find("input[name=avatarUrl]").val(item.avatarUrl);
            obj.find("input[name=photoImg]").val(item.photoImg);
        }
    })
}
function updSelUser(vals,userId){
    var tablename = "非进思用户数据列表";
    var type=4;
    var remark = userId+"";
    addOperLog(tablename,type,remark);
    $.ajax({
        url:"/personnel/updSelUser",
        type:"post",
        dataType:"json",
        data:vals,
        success:function(item) {
            if(item){
                selIndexUserTab();
                layer.msg("编译成功", {icon: 1})
            }else{
                layer.msg("编译失败", {icon: 2})
            }
        }
    })
}
/**
 *  管理停用开关
 */
function checkswitchoff(userId,account,sta){
    if(sta==3){
        layer.msg("账户已经停用");
    }else{
        layer.open({
            type:2,//弹出层的类型
            title:"封停用户",
            area:['550px','400px'],//弹出层大小
            shade:0,//设置遮罩
            offset:'auto',//设置遮罩的位置
            maxmin:true,
            //弹出层内容
            content :"/personnel/layer/PerUserList_layer_checkswitchoff.html",
            success:function(layero,index){
                //查找当前引入的iframe的内容
                var obj = layero.find('iframe').contents();
                obj.find("input[name=userId]").val(userId);
                obj.find("input[name=account]").val(account);
                var user=localStorage.getItem("user");
                user=JSON.parse(user);//将获取json字符串转json对象
                var implementId = user.userId;
                obj.find("input[name=implementId]").val(implementId);
            },
            btn:['确定','取消'],
            yes:function(index,layero){
                var body = layero.find('iframe').contents();
                var form = $(body).find('form').serialize();
                var type = 2;
                addClosureLog(userId,type);
                addClosure(form);
                layer.close(layer.index);
            },btn2:function(index,layero){

            }
        })
    }
}
function addClosure(vals){
    //更改用户状态 为3（停用）
    //添加停用日志  和添加停用的用户
    vals+="&type="+2
    $.ajax({
        url:"/personnel/addClosure",
        type:"post",
        dataType:"json",
        data:vals,
        success:function(data){
            if(data){
                layer.msg("封停成功", {icon: 1});
                selIndexUserTab();
            }else{
                layer.msg("封停失败", {icon: 2});
                selIndexUserTab();
            }
        }
    })
}
/**
 *  添加后台操作日志
 * */
function addOperLog(tablename,type,remark){
    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    var mid = create.userId;
    form+="&mid="+mid;
    form+="&tablename="+tablename;
    form+="&type="+type;
    form+="&remark="+remark;
    $.post("/personnel/addOperationLog", form, function (res) {
        form="";
    });
}
/**
 *  添加封停操作日志
 * */
function addClosureLog(userId,type){
    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    var operationId = create.userId;
    form+="&operationId="+operationId;
    form+="&userId="+userId;
    form+="&type="+type;
    $.post("/personnel/addClosureLog", form, function (res) {
        form="";
    });
}